home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 7: Sunsite / Linux Cubed Series 7 - Sunsite Vol 1.iso / system / network / manageme / tcpdump-.001 / tcpdump-~ / tcpdump-3.0.2-linux / libpcap-0.0.6 / inet.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-06-07  |  5.7 KB  |  197 lines

  1. /*
  2.  * Copyright (c) 1994
  3.  *    The Regents of the University of California.  All rights reserved.
  4.  *
  5.  * Redistribution and use in source and binary forms, with or without
  6.  * modification, are permitted provided that the following conditions
  7.  * are met:
  8.  * 1. Redistributions of source code must retain the above copyright
  9.  *    notice, this list of conditions and the following disclaimer.
  10.  * 2. Redistributions in binary form must reproduce the above copyright
  11.  *    notice, this list of conditions and the following disclaimer in the
  12.  *    documentation and/or other materials provided with the distribution.
  13.  * 3. All advertising materials mentioning features or use of this software
  14.  *    must display the following acknowledgement:
  15.  *    This product includes software developed by the Computer Systems
  16.  *    Engineering Group at Lawrence Berkeley Laboratory.
  17.  * 4. Neither the name of the University nor of the Laboratory may be used
  18.  *    to endorse or promote products derived from this software without
  19.  *    specific prior written permission.
  20.  *
  21.  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  22.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  23.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  24.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  25.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  26.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  27.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  28.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  29.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  30.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  31.  * SUCH DAMAGE.
  32.  */
  33.  
  34. #ifndef lint
  35. static char rcsid[] =
  36.     "@(#) $Header: inet.c,v 1.4 94/06/07 01:16:50 leres Exp $ (LBL)";
  37. #endif
  38.  
  39. #include <sys/param.h>
  40. #include <sys/file.h>
  41. #include <sys/ioctl.h>
  42. #include <sys/socket.h>
  43. #ifdef SOLARIS
  44. #include <sys/sockio.h>
  45. #endif
  46.  
  47. #include <net/if.h>
  48. #include <netinet/in.h>
  49.  
  50. #include <ctype.h>
  51. #include <errno.h>
  52. #include <stdio.h>
  53. #include <stdlib.h>
  54. #include <string.h>
  55. #include <unistd.h>
  56. #include <pcap.h>
  57.  
  58. /* Not all systems have IFF_LOOPBACK */
  59. #ifdef IFF_LOOPBACK
  60. #define ISLOOPBACK(p) ((p)->ifr_flags & IFF_LOOPBACK)
  61. #else
  62. #define ISLOOPBACK(p) (strcmp((p)->ifr_name, "lo0") == 0)
  63. #endif
  64.  
  65. /*
  66.  * Return the name of a network interface attached to the system, or NULL
  67.  * if none can be found.  The interface must be configured up; the
  68.  * lowest unit number is preferred; loopback is ignored.
  69.  */
  70. char *
  71. pcap_lookupdev(errbuf)
  72.     register char *errbuf;
  73. {
  74.     register int fd, minunit, n;
  75.     register char *cp;
  76.     register struct ifreq *ifrp, *ifend, *ifnext, *mp;
  77.     struct ifconf ifc;
  78.     struct ifreq ibuf[16], ifr;
  79.     static char device[sizeof(ifrp->ifr_name) + 1];
  80.  
  81.     fd = socket(AF_INET, SOCK_DGRAM, 0);
  82.     if (fd < 0) {
  83.         (void)sprintf(errbuf, "socket: %s", pcap_strerror(errno));
  84.         return (NULL);
  85.     }
  86.     ifc.ifc_len = sizeof ibuf;
  87.     ifc.ifc_buf = (caddr_t)ibuf;
  88.  
  89.     if (ioctl(fd, SIOCGIFCONF, (char *)&ifc) < 0 ||
  90.         ifc.ifc_len < sizeof(struct ifreq)) {
  91.         (void)sprintf(errbuf, "SIOCGIFCONF: %s", pcap_strerror(errno));
  92.         (void)close(fd);
  93.         return (NULL);
  94.     }
  95.     ifrp = ibuf;
  96.     ifend = (struct ifreq *)((char *)ibuf + ifc.ifc_len);
  97.  
  98.     mp = NULL;
  99.     minunit = 666;
  100.     for (; ifrp < ifend; ifrp = ifnext) {
  101. #if BSD - 0 >= 199006
  102.         n = ifrp->ifr_addr.sa_len + sizeof(ifrp->ifr_name);
  103.         if (n < sizeof(*ifrp))
  104.             ifnext = ifrp + 1;
  105.         else
  106.             ifnext = (struct ifreq *)((char *)ifrp + n);
  107.         if (ifrp->ifr_addr.sa_family != AF_INET)
  108.             continue;
  109. #else
  110.         ifnext = ifrp + 1;
  111. #endif
  112.         /*
  113.          * Need a template to preserve address info that is
  114.          * used below to locate the next entry.  (Otherwise,
  115.          * SIOCGIFFLAGS stomps over it because the requests
  116.          * are returned in a union.)
  117.          */
  118.         strncpy(ifr.ifr_name, ifrp->ifr_name, sizeof(ifr.ifr_name));
  119.         if (ioctl(fd, SIOCGIFFLAGS, (char *)&ifr) < 0) {
  120.             (void)sprintf(errbuf, "SIOCGIFFLAGS: %s",
  121.                 pcap_strerror(errno));
  122.             (void)close(fd);
  123.             return (NULL);
  124.         }
  125.  
  126.         /* Must be up and not the loopback */
  127.         if ((ifr.ifr_flags & IFF_UP) == 0 || ISLOOPBACK(&ifr))
  128.             continue;
  129.  
  130.         for (cp = ifrp->ifr_name; !isdigit(*cp); ++cp)
  131.             continue;
  132.         n = atoi(cp);
  133.         if (n < minunit) {
  134.             minunit = n;
  135.             mp = ifrp;
  136.         }
  137.     }
  138.     (void)close(fd);
  139.     if (mp == NULL) {
  140.         (void)strcpy(errbuf, "no suitable device found");
  141.         return (NULL);
  142.     }
  143.  
  144.     (void)strncpy(device, mp->ifr_name, sizeof(device) - 1);
  145.     device[sizeof(device) - 1] = '\0';
  146.     return (device);
  147. }
  148.  
  149. int
  150. pcap_lookupnet(device, netp, maskp, errbuf)
  151.     register char *device;
  152.     register u_long *netp, *maskp;
  153.     register char *errbuf;
  154. {
  155.     register int fd;
  156.     register struct sockaddr_in *sin;
  157.     struct ifreq ifr;
  158.  
  159.     fd = socket(AF_INET, SOCK_DGRAM, 0);
  160.     if (fd < 0) {
  161.         (void)sprintf(errbuf, "socket: %s", pcap_strerror(errno));
  162.         return (-1);
  163.     }
  164.     (void)strncpy(ifr.ifr_name, device, sizeof(ifr.ifr_name));
  165.     if (ioctl(fd, SIOCGIFADDR, (char *)&ifr) < 0) {
  166.         (void)sprintf(errbuf, "SIOCGIFADDR: %s: %s",
  167.             device, pcap_strerror(errno));
  168.         (void)close(fd);
  169.         return (-1);
  170.     }
  171.     sin = (struct sockaddr_in *)&ifr.ifr_addr;
  172.     *netp = sin->sin_addr.s_addr;
  173.     if (ioctl(fd, SIOCGIFNETMASK, (char *)&ifr) < 0) {
  174.         (void)sprintf(errbuf, "SIOCGIFNETMASK: %s: %s",
  175.             device, pcap_strerror(errno));
  176.         (void)close(fd);
  177.         return (-1);
  178.     }
  179.     (void)close(fd);
  180.     *maskp = sin->sin_addr.s_addr;
  181.     if (*maskp == 0) {
  182.         if (IN_CLASSA(*netp))
  183.             *maskp = IN_CLASSA_NET;
  184.         else if (IN_CLASSB(*netp))
  185.             *maskp = IN_CLASSB_NET;
  186.         else if (IN_CLASSC(*netp))
  187.             *maskp = IN_CLASSC_NET;
  188.         else {
  189.             (void)sprintf(errbuf, "inet class for 0x%x unknown",
  190.                 *netp);
  191.             return (-1);
  192.         }
  193.     }
  194.     *netp &= *maskp;
  195.     return (0);
  196. }
  197.